package com.wyzz.hr.util;

import java.util.*;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class QuartzFileConfigManager implements SysConfigManager {

	private QuartzFileConfigManager() {
		try {
			PropertiesConfiguration.setDefaultListDelimiter('!');
			config = new PropertiesConfiguration("sys_quartz.properties");
			config.setEncoding("utf-8");
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}

	public static QuartzFileConfigManager getInstance() {
		return instance;
	}

	public void addNewProperty(String key, String value) {
		config.clearProperty(key);
		config.addProperty(key, value);
		QUARTZ_CONFIG_PROPERTIES_CACHE.put(key, value);
	}

	public String getProperty(String key) {
		if (QUARTZ_CONFIG_PROPERTIES_CACHE.containsKey(key)) {
			return (String) QUARTZ_CONFIG_PROPERTIES_CACHE.get(key);
		} else {
			String value = config.getString(key);
			return value != null ? value : "";
		}
	}

	public void removeProperty(String key) {
		config.clearProperty(key);
		if (QUARTZ_CONFIG_PROPERTIES_CACHE.containsKey(key))
			QUARTZ_CONFIG_PROPERTIES_CACHE.remove(key);
	}

	public void setProperty(String key, String value) {
		config.setProperty(key, value);
		QUARTZ_CONFIG_PROPERTIES_CACHE.put(key, value);
	}

	public void saveChange() {
		try {
			config.save();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}

	public Map getProperties() {
		String key;
		for (Iterator it = config.getKeys(); it.hasNext(); QUARTZ_CONFIG_PROPERTIES_CACHE
				.put(key, (String) config.getProperty(key)))
			key = (String) it.next();

		return QUARTZ_CONFIG_PROPERTIES_CACHE;
	}

	private static final QuartzFileConfigManager instance = new QuartzFileConfigManager();
	private static final String QUARTZ_CONFIG_FILE_NAME = "sys_quartz.properties";
	private static final Map QUARTZ_CONFIG_PROPERTIES_CACHE = new HashMap();
	private static final String QUARTZ_CONFIG_DEFAULT_ENCODING = "utf-8";
	private PropertiesConfiguration config;
	private static final char DEFAULT_DELIMITER = 33;

}
